/*
  Part of the MasterTool3D project - http://code.google.com/p/master-tool-3d

  Copyright (c) 2011 Ingo Pueschl

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation, version 2.1.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package techjit.gfx.util.geo;

import processing.core.PApplet;
import techjit.gfx.util.PU;
import techjit.util.RuntimeParams;

public class Vector {

	// FIELDS
	private float x;
	private float y;
	private float z;

	// CONSTRUCTORS
	public Vector(float floatValue) {
		this(floatValue, floatValue, floatValue);
	}

	public Vector(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public Vector(Vector vector) {
		this(vector.x, vector.y, vector.z);
	}

	public Vector(Vector vector, float length) {
		this(0);
		vector.normalize();
		vector.multiply(length);
		this.change(vector);
	}

	// METHODS
	public void add(Vector vector) {
		x += vector.getX();
		y += vector.getY();
		z += vector.getZ();
	}

	public void change(Vector vector) {
		x = vector.x;
		y = vector.y;
		z = vector.z;
	}

	public void draw(PApplet p) {
		p.pushMatrix();
		p.pushStyle();
		{
			p.stroke(RuntimeParams.COLOR__MAGENTA);
			PU.line(p, new Vector(0), this);
		}
		p.popMatrix();
		p.popStyle();
	}

	public Vector getDifference(Vector vector) {
		return new Vector(x - vector.x, y - vector.y, z - vector.z);
	}

	public Vector getInverse() {
		return new Vector(-x, -y, -z);
	}

	public float getLength() {
		return PApplet.sqrt(x * x + y * y + z * z);
	}

	public Vector getMultiple(float floatValue) {
		return new Vector(x * floatValue, y * floatValue, z * floatValue);
	}

	public Vector getSum(Vector vector) {
		return new Vector(x + vector.x, y + vector.y, z + vector.z);
	}

	public float getX() {
		return x;
	}

	public float getY() {
		return y;
	}

	public float getZ() {
		return z;
	}

	public void invert() {
		x = -x;
		y = -y;
		z = -z;
	}

	public void multiply(float floatValue) {
		x = floatValue * x;
		y = floatValue * y;
		z = floatValue * z;
	}

	public void normalize() {
		float length = getLength();
		multiply(1 / length);
	}

	public void subtract(Vector vector) {
		x -= vector.getX();
		y -= vector.getY();
		z -= vector.getZ();
	}

	@Override
	public String toString() {
		return x + "|" + y + "|" + z;
	}
}
